package dr.incomeapp.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import dr.incomeapp.R;
import dr.incomeapp.bean.IncomeBean;
import dr.incomeapp.model.FactoryModel;
import dr.incomeapp.model.IncomeManagerModel;
import dr.incomeapp.view.ListRowAdapterSearch;
import dr.incomeapp.view.miniView.LvTitleMiniView;



public class IncomeManagerController extends MyController implements OnItemClickListener, OnScrollListener {

	
	protected ListView lvIncome;
	protected Button btnSearch;
	protected Button btnAddIncome;
	
	protected Button btnDatePrev;
	protected Button btnDateNext;
	protected Button btnDateDialog;
	protected Button btnDate;
	
	protected TextView tvMoney;
	protected TextView tvMoneyProg;

	
	protected ListRowAdapterSearch lraJoke;
	
	DatePicker dpDate;
	
	protected IncomeManagerModel model = FactoryModel.getIncomeManagerModel();
	
	 @Override
		public void onCreate(Bundle savedInstanceState) {
		 
		        super.onCreate(savedInstanceState);
		        
		        model.addObserver(this);
		        setContentView(R.layout.income_manager);

	       
		        btnSearch 			= (Button)findViewById(R.id.btnSearch);
		        btnAddIncome 		= (Button)findViewById(R.id.btnAddIncome);
		        btnDatePrev 		= (Button)findViewById(R.id.btnDatePrev);
		        btnDateNext 		= (Button)findViewById(R.id.btnDateNext);
		        btnDate 			= (Button) findViewById(R.id.btnDate);
		        
		        lvIncome 			= (ListView)findViewById(R.id.lvJoke);
		        
		        tvMoney = (TextView) findViewById(R.id.tvMoney);
		        tvMoneyProg = (TextView) findViewById(R.id.tvMoneyProg);

		        
		        lvIncome.setOnItemClickListener(this);
		        
		        
		        lvIncome.setOnScrollListener(this);
		        lraJoke = new ListRowAdapterSearch(this);
			    lraJoke.setTitleView(new LvTitleMiniView(this));

		        //lraJoke.setList(model.getListIncome());
		        
		        btnSearch.setOnClickListener(this);
		        btnAddIncome.setOnClickListener(this);
			    btnDatePrev.setOnClickListener(this);
			    btnDateNext.setOnClickListener(this);
			    btnDate.setOnClickListener(this);
		        
		        lvIncome.setAdapter(lraJoke);
		    
		        
		        
		        init();
		        setValues();
		 }
	 @Override
	protected void onResume() {
		
		super.onResume();
		
		setValues();
	}
	 
	 public void setValues(){
		 
		 SimpleDateFormat df = new SimpleDateFormat("MMMMMMMMMM, yyyy");
		 
		 btnDate.setText(df.format(model.getDate()));
		 
		 float money = model.getMoney();
		 
		 tvMoney.setText(String.valueOf(money));
		 
		 if(money < 0){
			tvMoney.setTextColor(getResources().getColor(R.color.colorred));
		 }else{
			 tvMoney.setTextColor(getResources().getColor(R.color.colorgreen));
		 }
		 
		 
		 float moneyProg = model.getMoneyProg();
		 
		 tvMoneyProg.setText(String.valueOf(moneyProg));
		 
		 if(money > moneyProg){
			 tvMoneyProg.setTextColor(getResources().getColor(R.color.colorred));
		 }else{
			 tvMoneyProg.setTextColor(getResources().getColor(R.color.colorgreen));
		 }
		 
		lraJoke.setList(model.getListIncome());
		lvIncome.setAdapter(lraJoke);
		 
	 }
	 
		@Override
	public void onClick(View v) {
		super.onClick(v);	
			if(btnSearch.equals(v)){
				model.clear();
				model.refresh();
				

				lraJoke.setList(model.getListIncome());
				lvIncome.setAdapter(lraJoke);
					
			}else if(btnAddIncome.equals(v)){
				Intent intent = new Intent(this, IncomeEditController.class);
				startActivity(intent);	
			}else if(btnDateNext.equals(v)){
				model.calcNextMonthIncome();
				setValues();
				
			}else if(btnDatePrev.equals(v)){
				model.calcPrevMonthIncome();
				setValues();
			}else if(btnDate.equals(v)){
				dpDate = Util.createDatePickerDialog(this, model.getDate());
			}
			
			
		
	}
		
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			// TODO Auto-generated method stub
			
			List<IncomeBean> listIncomeBean = model.getListIncome();
			
			//oneline?
			if(position == listIncomeBean.size()){
				return;
			}
		
			IncomeBean incomeBean = listIncomeBean.get(position);
			

			
			Intent intent = new Intent(this, IncomeViewController.class);
			intent.putExtra(IncomeBean.BEANNAME, incomeBean);
		
			startActivity(intent);
			
		}	
		

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	
	public void onDateSet(DatePicker dp, int year, int monthOfYear,
			int dayOfMonth) {
		super.onDateSet(dp, year, monthOfYear, dayOfMonth);
	
		Calendar ca2 = Calendar.getInstance();
		ca2.set(year, monthOfYear, dayOfMonth);
		
		Date da = ca2.getTime();
		
		model.setDate(da);

		setValues();

	}

}
